package com.alan.wifishare.presenter.proxy;

import com.alan.libapi.business.IBusinessHttp;
import com.alan.libapi.business.dao.CallbackListener;
import com.alan.libapi.model.business.UserSession;
import com.alan.libapi.model.persistent.CmdNames;
import com.alan.libapi.model.persistent.ErrorInfo;
import com.alan.libapi.model.persistent.ErrorSets;
import com.alan.library.allog.KLog;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.app.UserConfig;
import com.alan.wifishare.utils.BusinessUtil;

import java.lang.reflect.Method;

/**
 * @className: BusinessHttpProxy
 * @describtion: BusinessHttpProxy
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/5/6 17:43
 */
public class BusinessHttpProxy extends ALAbstractProxy<IBusinessHttp> {

    private static final ErrorInfo HAVE_NOT_LOGIN_ERROR = new ErrorInfo(ErrorSets.CODE_HAVE_NOT_LOGIN, ErrorSets.MSG_HAVE_NOT_LOGIN);
    private boolean isNeedToDealNotLogin = false;
    private CallbackListener<UserSession> loginCallbackListener = new CallbackListener<UserSession>() {
        @Override
        public void onSuccess(UserSession result) {
            if(result != null){
                GlobalField.userSession = result;
            }
        }
        @Override
        public void onFailure(int errorCode, String msg) {
            KLog.e(msg);
        }
    };
    public BusinessHttpProxy(IBusinessHttp targert) {
        super(targert);
    }

    @Override
    protected void doBefore(IBusinessHttp object, Method method, Object[] objects) {
        isNeedToDealNotLogin = false;
        boolean isNeedLogin = isNeedLogin(method.getName());
        KLog.e("Before--->>> the method " + method.getName() + "() --->>> isNeedLogin--->>>" + isNeedLogin);
        if(isNeedLogin && !isHaveLogin()){//如果该命令需要登录，但是用户还未登录成功则不允许执行
            setAllowInvoke(false);
            isNeedToDealNotLogin = true;
        }
    }

    @Override
    protected void doAfter(IBusinessHttp object, Method method, Object[] objects) {
        KLog.e("After--->>> the method " + method.getName() + "() --->>> isAllowInvoked = " + isAllowInvoke() + " --->>> isNeedToDealNotLogin = " + isNeedToDealNotLogin);
        if(isNeedToDealNotLogin){
            CallbackListener callbackListener = (CallbackListener) objects[objects.length-1];//IBusinessHttp接口所有参数最后一个均为CallbackListener
            callbackListener.onFailure(HAVE_NOT_LOGIN_ERROR.getErrorCode(), HAVE_NOT_LOGIN_ERROR.getErrorMsg());
            if(BusinessUtil.isNetworkConnected(GlobalField.context)){
                String userAccount = GlobalField.userConfig.getUserAccount();
                String userPwd = GlobalField.userConfig.getUserPwd();
                GlobalField.businessHttp.postUserLogin(userAccount, userPwd, loginCallbackListener);//如果网络连接成功则尝试自动登录
            }
        }
    }

    /**
     * 根据方法名判断当前命令是否需要登录
     * @param curMethodName
     * @return
     */
    private boolean isNeedLogin(String curMethodName){
        if(curMethodName.startsWith("get")){
            for (String methodName:CmdNames.NEED_LOGIN_GET_MEDTHODS) {
                if(curMethodName.equals(methodName)){
                    return true;
                }
            }
        }else if(curMethodName.startsWith("post")){
            for (String methodName:CmdNames.NEED_LOGIN_POST_MEDTHODS) {
                if(curMethodName.equals(methodName)){
                    return true;
                }
            }
        }
        return false;
    }
    
    
    private boolean isHaveLogin(){
        if(GlobalField.userSession != null){
            return true;
        }
        return false;
    }

}
